home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 026-050 / scopedisk45 / edible2 / edible.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  114 lines

  1. /**************************************************************************\
  2. *     Written on November 30, 1988 by Christopher J. Dyl (GEnie: C.DYL)    *
  3. *  If you are like me, you like to use ED all the time.  ED is a nice and  *
  4. *  small editor and I use it a lot.  The problem with ED is that you will  *
  5. *  constantly get a "File contains binary" message when it really only has *
  6. *  a few control characters.  This program will do many things to modify a *
  7. *  file in order to make it EDible.  First, it will remove all characters  *
  8. *  that ED cannot handle.  It will also remove the CR/LFs of many files as *
  9. *  when using ED, only LFs are needed and CR/LFs will leave lots of messy  *
  10. *  double spacing in the file.  Another nice feature is the BS (BACKSPACE) *
  11. *   handler.  If you have ever captured an online session from a BBS, you  *
  12. *  will appreciate this function.  It will take out the backspaces in the  *
  13. *  file and move back 1 space in the file so it can be replaced with the   *
  14. *  next character.  If the next character is a backspace, it will continue *
  15. *  to go back as many spaces as it takes until it starts printing forward  *
  16. *  again.  Another feature is replacing a TAB with 8 spaces.  Since TABs   *
  17. *  are many times 5 spaces, I just thought this would be easier and since  *
  18. *  this is my first C program, I figured I would keep it simple.  To get   *
  19. *  usage of the program, just type EDible.  An optional third comment may  *
  20. *  be added if you like.  This can be a number from 0-255.  It will tell   *
  21. *   the program the maximum line length you want.  Leaving it blank will   *
  22. *  disable this function.  If you really like this program and would like  *
  23. *  to make any sort of donation, please send it to:                        *
  24. *    Christopher Dyl                 GEnie Address:  C.DYL                 *
  25. *    191 Agricultural Avenue          Phone Number:  (508)/222-8320        *
  26. *    Rehoboth, MA  02769             Compiled using Manx Aztec 3.6a        *
  27. *  Any amout will be greatly appreciated and will convince me to write yet *
  28. *  more programs!  If you have any ideas of programs you have always have  *
  29. *  liked to have, please send me a letter at the same address.  I am a new *
  30. *  programmer and and looking forward to programming ideas from the people *
  31. *  themselves so I can know what they would like!  Thank you for any money *
  32. *  you donate.  I am currently rather broke and ANY amount of money will   *
  33. *  help!                                                                   *
  34. \**************************************************************************/
  35.  
  36. #include <stdio.h>
  37.  
  38. #define CR  0x0D            /* Carriage Return */
  39. #define LF  0x0A            /* Line Feeds */
  40. #define SP  0x20            /* SPace */
  41. #define BS  0x08            /* BackSpace */
  42. #define DEL 0x7F            /* DELete */
  43. #define TAB 0x09            /* TAB */
  44. #define LOW 0xA0            /* LOWest alternate character */
  45. #define HI  0xFF            /* HIghest alternate character */
  46.  
  47. main(argc, argv)
  48.   int argc;
  49.   char *argv[];
  50. {
  51. int max = 0, len = 0, chr = 0, lc = 0;
  52. int c, x, lines = 0, tchr = 0;
  53.  
  54. FILE *fp1, *fp2, *fopen();
  55.  
  56.   if (argc < 3)
  57.   { printf("Usage: EDible input output [Max Line Length]\n");
  58.     exit(1);
  59.   }
  60.   else if ((fp1 = fopen(argv[1], "r")) == NULL)
  61.   { printf("Cannot open file %s\n", argv[1]);
  62.     exit(1);
  63.   }
  64.   else if ((fp2 = fopen(argv[2], "x+")) == NULL)
  65.   { printf("Cannot create file %s\n", argv[2]);
  66.     exit(1);
  67.   }
  68.   else
  69.     printf("Making file %s usable with ED as file %s\n", argv[1], argv[2]);
  70.   if (argc > 3)
  71.     sscanf(argv[3], "%d", &max);         /* Convert argv[3] to a decimal */
  72.  
  73. {
  74.  while ((c = getc(fp1)) != EOF)
  75.  { if ((c == CR) ||
  76.        (c == LF && lc != CR) ||          /* Eliminates CR/LFs together */
  77.        (c >= SP && c < DEL) ||
  78.        (c >= LOW && c <= HI))            /* Check weeding conditions */
  79.    { putc(c, fp2);                       /* put character in file */
  80.      chr++;
  81.      if (++len == max)                   /* force a linefeed if the line */
  82.      { len = 0;                          /* is greater than the maximum */
  83.        lines++;
  84.        putc(LF, fp2);
  85.      }
  86.      else if (len > 512)                 /* got an overflow so this is to */
  87.        len = 0;                          /* prevent that */
  88.    }
  89.      if (c == LF || c == CR)             /* set length to 0 if a LF or CR */
  90.      { len = 0;                          /* is in the file */
  91.        lines++;
  92.      }
  93.    else if (c == TAB)
  94.    { for (x=0; x < 8; x++)               /* extremely cheap tab handler */
  95.        putc(SP, fp2);
  96.      tchr++;
  97.      chr =+ 5;                           /* count total characters weeded */
  98.    }
  99.    else if (c == BS)
  100.    { fseek(fp2, -1L, 1);                 /* very good backspace handler */
  101.      tchr++;
  102.    }
  103.    lc = c;                               /* put character to variable */
  104.    tchr++;
  105.  }
  106. }
  107.  
  108. fclose(fp1);                 /* Close all files and then print results */
  109. fclose(fp2);
  110. printf("\nA total of %d characters were replaced.", chr);
  111. printf("\nA total of %d characters were weeded.", tchr);
  112. printf("\nThere are %d lines in the file.\n\n", lines);
  113. }
  114.